﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using System.Reflection;
using Zero.Data.Repositories;
using Zero.Data.Infrastructure;
using System.Web.Http;
using Zero.Service;


namespace Zero.Web
{
    public static class Bootstrapper
    {
        public static void Run()
        {
            SetAutofacContainers();
          
        }

        private static void SetAutofacContainers()
        {
           
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
            builder.RegisterType<SystemService>().As<ISystemService>().InstancePerHttpRequest();
            builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerHttpRequest();
            builder.RegisterType<MenuRepository>().As<IMenuRepository>().InstancePerHttpRequest();
            builder.RegisterType<MenuService>().As<IMenuService>().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(typeof(UserRepository)
                .Assembly).Where(t => t.Name.EndsWith("Repository"))
                .AsImplementedInterfaces().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(typeof(SystemService)
                .Assembly).Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces().InstancePerHttpRequest();
            //var services = Assembly.Load("Zero.Service");
            //builder.RegisterAssemblyTypes(services).Where(t => t.Name.EndsWith("Service"))
            //    .InstancePerApiRequest();
            builder.RegisterFilterProvider();
            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}